home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-09-24 | 3.4 KB | 160 lines | [TEXT/MPS ] |
- (* filename.mlp *)
-
- (**) #open "int";;
- (**) #open "eq";;
- (**) #open "fstring";;
- (**) #open "exc";;
-
- let check_suffix name suff =
- string_length name >= string_length suff &
- sub_string name (string_length name - string_length suff) (string_length suff)
- = suff
- ;;
-
- let chop_suffix name suff =
- sub_string name 0 (string_length name - string_length suff)
- ;;
-
- #ifdef unix
- let current_dir_name = ".";;
-
- let concat dirname filename =
- let l = string_length dirname - 1 in
- if l < 0 or nth_char dirname l == `/`
- then dirname ^ filename
- else dirname ^ "/" ^ filename
- ;;
-
- let is_absolute n =
- (string_length n >= 1 & sub_string n 0 1 = "/")
- or (string_length n >= 2 & sub_string n 0 2 = "./")
- or (string_length n >= 3 & sub_string n 0 3 = "../")
- ;;
-
- let slash_pos s =
- let rec pos i =
- if i < 0 then raise Not_found
- else if nth_char s i == `/` then i
- else pos (i - 1)
- in pos (string_length s - 1)
- ;;
-
- let basename name =
- try
- let p = slash_pos name + 1 in
- sub_string name p (string_length name - p)
- with Not_found ->
- name
- ;;
-
- let dirname name =
- if name = "/" then name else
- try
- sub_string name 0 (slash_pos name)
- with Not_found ->
- "."
- ;;
- #endif
-
- #ifdef macintosh
- let current_dir_name = ":";;
-
- let is_absolute n =
- try
- for i = 0 to string_length n - 1 do
- if nth_char n i == `:` then raise Exit
- done;
- false
- with Exit ->
- true
- ;;
-
- let concat dirname filename =
- let dirname1 =
- if is_absolute dirname
- then dirname
- else ":" ^ dirname in
- let l =
- string_length dirname1 - 1 in
- let dirname2 =
- if l < 0 or nth_char dirname1 l == `:`
- then dirname1
- else dirname1 ^ ":" in
- let filename2 =
- if string_length filename > 0 & nth_char filename 0 == `:`
- then sub_string filename 1 (string_length filename - 1)
- else filename in
- dirname2 ^ filename2
- ;;
-
- let colon_pos s =
- let rec pos i =
- if i < 0 then raise Not_found else
- if nth_char s i == `:` then i else pos (i - 1)
- in pos (string_length s - 1)
- ;;
-
- let basename name =
- try
- let p = colon_pos name + 1 in
- sub_string name p (string_length name - p)
- with Not_found ->
- name
- ;;
-
- let dirname name =
- if name = ":" then name else
- try
- sub_string name 0 (colon_pos name)
- with Not_found ->
- ":"
- ;;
- #endif
-
- #ifdef msdos
- let current_dir_name = ".";;
-
- let concat dirname filename =
- let l = string_length dirname - 1 in
- if l < 0 or nth_char dirname l == `\\` or nth_char dirname l == `:`
- then dirname ^ filename
- else dirname ^ "\\" ^ filename
- ;;
-
- let is_absolute n =
- (string_length n >= 2 & nth_char n 1 == `:`)
- or (string_length n >= 1 & sub_string n 0 1 = "\\")
- or (string_length n >= 2 & sub_string n 0 2 = ".\\")
- or (string_length n >= 3 & sub_string n 0 3 = "..\\")
- ;;
-
- let sep_pos s =
- let rec pos i =
- if i < 0 then raise Not_found else
- match nth_char s i with
- `/` | `\\` | `:` -> i
- | _ -> pos (i - 1)
- in pos (string_length s - 1)
- ;;
-
- let basename name =
- try
- let p = sep_pos name + 1 in
- sub_string name p (string_length name - p)
- with Not_found ->
- name
- ;;
-
- let rec dirname name =
- if string_length name >= 2 & nth_char name 1 == `:` then
- sub_string name 0 2 ^ dirname (sub_string name 2 (string_length name - 2))
- else if name = "/" or name = "\\" then
- name
- else
- try
- sub_string name 0 (sep_pos name)
- with Not_found ->
- "."
- ;;
- #endif
-